package com.smyh.UIM;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.smyh.bean.RFDeviceInfo;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.CMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.future.WriteFuture;

import java.util.ArrayList;
import java.util.List;

public class TerminalParam extends AppCompatActivity {

    public static List<String> terminalParams = null;

    AlertDialog.Builder builder;
    TextView textView;
    ListView listView;

   static String addr ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_terminal_param);
        builder = new AlertDialog.Builder(TerminalParam.this);
        addr =  getIntent().getStringExtra("addr");
        builder.setTitle("地址:"+addr);
        initData();
        initView();
        initEvent();
    }
    EditText editText2,editText3,editText8,editText1;
    EditText field1_param2_workFreq,field2_param2_ConectDist,field3_param2_reciveTime,field4_param2_deviceType;
    EditText field1_param3_netAddr1,field2_param3_netAddr2,field3_param3_phaseAddr;
    EditText field1_param8_pw1, field2_param8_pw2,field3_param8_pw3 , field4_param8_pw4, field5_param8_pw5 , field6_param8_pw6,
             field7_param8_data1, field8_param8_data2  , field9_param8_data3 ,field10_param8_data4;
    private void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                View view1;
                View view2;
                View view3;
                View view8;
                String content = terminalParams.get(position);
                byte[] contentBytes = new byte[0];
                if(!content.contains("--")){
                    contentBytes = HexStrUtil.hexStr2Bytes(content);
                }

                switch (position+1){
                    case 2:
                        view2 = View.inflate(TerminalParam.this,R.layout.activity_terminal_param_2,null);
                        editText2 = (EditText) view2.findViewById(R.id.content);
                        editText2.setText(content);
                        editText2.setSelection(editText2.getText().length());
                        if(contentBytes.length == 20){
                            byte[] f1 = {contentBytes[1]};
                            byte[] f2 = {contentBytes[2]};
                            byte[] f3 = {contentBytes[3]};
                            byte[] f4 = {contentBytes[8]};
                            field1_param2_workFreq = (EditText) view2.findViewById(R.id.workFreq);
                            field2_param2_ConectDist = (EditText) view2.findViewById(R.id.conectDist);
                            field3_param2_reciveTime = (EditText) view2.findViewById(R.id.reciveTime);
                            field4_param2_deviceType = (EditText) view2.findViewById(R.id.deviceType);

                            field1_param2_workFreq.setText(HexStrUtil.byte2HexStr(f1));
                            field1_param2_workFreq.setSelection(field1_param2_workFreq.getText().length());

                            field2_param2_ConectDist.setText(HexStrUtil.byte2HexStr(f2));
                            field2_param2_ConectDist.setSelection(field2_param2_ConectDist.getText().length());

                            field3_param2_reciveTime.setText(HexStrUtil.byte2HexStr(f3));
                            field3_param2_reciveTime.setSelection(field3_param2_reciveTime.getText().length());

                            field4_param2_deviceType.setText(HexStrUtil.byte2HexStr(f4));
                            field4_param2_deviceType.setSelection(field4_param2_deviceType.getText().length());
                        }
                        builder.setView(view2);
                        break;
                    case 3:
                        view3 = View.inflate(TerminalParam.this,R.layout.activity_terminal_param_3,null);
                        editText3 = (EditText)  view3.findViewById(R.id.content);
                        editText3.setText(content);
                        editText3.setSelection(editText3.getText().length());

                        if(contentBytes.length == 20){
                            byte[] f1 = {contentBytes[2]};
                            byte[] f2 = {contentBytes[3]};
                            byte[] f3 = {contentBytes[6]};
                            field1_param3_netAddr1 = (EditText) view3.findViewById(R.id.netAddr1);
                            field2_param3_netAddr2 = (EditText) view3.findViewById(R.id.netAddr2);
                            field3_param3_phaseAddr = (EditText) view3.findViewById(R.id.phaseAddr);

                            field1_param3_netAddr1.setText(HexStrUtil.byte2HexStr(f1));
                            field1_param3_netAddr1.setSelection(field1_param3_netAddr1.getText().length());

                            field2_param3_netAddr2.setText(HexStrUtil.byte2HexStr(f2));
                            field2_param3_netAddr2.setSelection(field2_param3_netAddr2.getText().length());

                            field3_param3_phaseAddr.setText(HexStrUtil.byte2HexStr(f3));
                            field3_param3_phaseAddr.setSelection(field3_param3_phaseAddr.getText().length());
                        }

                        builder.setView(view3);
                        break;
                    case 8:
                        view8 = View.inflate(TerminalParam.this,R.layout.activity_terminal_param_8,null);
                        editText8 = (EditText)  view8.findViewById(R.id.content);
                        editText8.setText(terminalParams.get(position));
                        editText8.setSelection(editText8.getText().length());

                        if(contentBytes.length == 20){
                            byte[] f1 = {contentBytes[1]};
                            byte[] f2 = {contentBytes[2]};
                            byte[] f3 = {contentBytes[3]};
                            byte[] f4 = {contentBytes[4]};
                            byte[] f5 = {contentBytes[5]};
                            byte[] f6 = {contentBytes[6]};
                            byte[] f7 = {contentBytes[7]};
                            byte[] f8 = {contentBytes[8]};
                            byte[] f9 = {contentBytes[9]};
                            byte[] f10 = {contentBytes[10]};
                            field1_param8_pw1 = (EditText) view8.findViewById(R.id.pw1);
                            field2_param8_pw2 = (EditText) view8.findViewById(R.id.pw2);
                            field3_param8_pw3 = (EditText) view8.findViewById(R.id.pw3);
                            field4_param8_pw4 = (EditText) view8.findViewById(R.id.pw4);
                            field5_param8_pw5 = (EditText) view8.findViewById(R.id.pw5);
                            field6_param8_pw6 = (EditText) view8.findViewById(R.id.pw6);
                            field7_param8_data1 = (EditText) view8.findViewById(R.id.data1);
                            field8_param8_data2 = (EditText) view8.findViewById(R.id.data2);
                            field9_param8_data3 = (EditText) view8.findViewById(R.id.data3);
                            field10_param8_data4 = (EditText) view8.findViewById(R.id.data4);

                            field1_param8_pw1.setText(HexStrUtil.byte2HexStr(f1));
                            field1_param8_pw1.setSelection(field1_param8_pw1.getText().length());
                            field2_param8_pw2.setText(HexStrUtil.byte2HexStr(f2));
                            field2_param8_pw2.setSelection(field2_param8_pw2.getText().length());
                            field3_param8_pw3.setText(HexStrUtil.byte2HexStr(f3));
                            field3_param8_pw3.setSelection(field3_param8_pw3.getText().length());
                            field4_param8_pw4.setText(HexStrUtil.byte2HexStr(f4));
                            field4_param8_pw4.setSelection(field4_param8_pw4.getText().length());
                            field5_param8_pw5.setText(HexStrUtil.byte2HexStr(f5));
                            field5_param8_pw5.setSelection(field5_param8_pw5.getText().length());
                            field6_param8_pw6.setText(HexStrUtil.byte2HexStr(f6));
                            field6_param8_pw6.setSelection(field6_param8_pw6.getText().length());
                            field7_param8_data1.setText(HexStrUtil.byte2HexStr(f7));
                            field7_param8_data1.setSelection(field7_param8_data1.getText().length());
                            field8_param8_data2.setText(HexStrUtil.byte2HexStr(f8));
                            field8_param8_data2.setSelection(field8_param8_data2.getText().length());
                            field9_param8_data3.setText(HexStrUtil.byte2HexStr(f9));
                            field9_param8_data3.setSelection(field9_param8_data3.getText().length());
                            field10_param8_data4.setText(HexStrUtil.byte2HexStr(f10));
                            field10_param8_data4.setSelection(field10_param8_data4.getText().length());
                        }

                        builder.setView(view8);
                        break;
                    default:
                        view1 = View.inflate(TerminalParam.this,R.layout.activity_terminal_param_1,null);
                        editText1 = (EditText)   view1.findViewById(R.id.content);
                        editText1.setText(terminalParams.get(position));
                        editText1.setSelection(editText1.getText().length());
                        builder.setView(view1);
                        break;
                }
                //确定按钮
                builder.setPositiveButton("刷新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        if(MinaClientThread.session.isConnected()){
                            WriteFuture wf = MinaClientThread.session.write(CMDUtil.askDeviceParam(HexStrUtil.hexStr2Bytes(addr), (byte) (position+1)));
                            try {
                                boolean rs = wf.await(500);
                                if(!rs){
                                    MinaClientThread.session.resumeWrite();
                                    MinaClientThread.session.write(CMDUtil.askDeviceParam(HexStrUtil.hexStr2Bytes(addr), (byte) (position+1)));
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                MinaClientThread.session.resumeWrite();
                                MinaClientThread.session.write(CMDUtil.askDeviceParam(HexStrUtil.hexStr2Bytes(addr), (byte) (position+1)));

                            }
                        }
                    }
                });
                //否定按钮
                builder.setNegativeButton("整定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        byte[] contentBytes = new byte[20];
                        String content = "";
                        switch (position+1){
                            case 2:
                                content = editText2.getText().toString();
                                byte[] getBytes2 =HexStrUtil.hexStr2Bytes(content);
                                for(int i=1;i<getBytes2.length;i++){
                                    contentBytes[i] = getBytes2[i];
                                }
                                byte f21 = HexStrUtil.hexStr2Bytes(field1_param2_workFreq.getText().toString())[0];
                                byte f22 = HexStrUtil.hexStr2Bytes(field2_param2_ConectDist.getText().toString())[0];
                                byte f23 = HexStrUtil.hexStr2Bytes(field3_param2_reciveTime.getText().toString())[0];
                                byte f24 = HexStrUtil.hexStr2Bytes(field4_param2_deviceType.getText().toString())[0];
                                contentBytes[1] = f21;
                                contentBytes[2] = f22;
                                contentBytes[3] = f23;
                                contentBytes[8] = f24;

                                contentBytes[0] = 2;
                                break;
                            case 3:
                                content = editText3.getText().toString();
                                byte[] getBytes3 =HexStrUtil.hexStr2Bytes(content);
                                for(int i=1;i<getBytes3.length;i++){
                                    contentBytes[i] = getBytes3[i];
                                }
                                byte f31 = HexStrUtil.hexStr2Bytes(field1_param3_netAddr1.getText().toString())[0];
                                byte f32 = HexStrUtil.hexStr2Bytes(field2_param3_netAddr2.getText().toString())[0];
                                byte f33 = HexStrUtil.hexStr2Bytes(field3_param3_phaseAddr.getText().toString())[0];
                                contentBytes[2] = f31;
                                contentBytes[3] = f32;
                                contentBytes[6] = f33;
                                contentBytes[0] = 3;
                                RFDeviceInfo obj = MainCahce.deviceInfos.get(addr);
                                byte[] groupAddr = obj.getGroupAddr();
                                if(groupAddr!=null){
                                    groupAddr[0] = f31;
                                    groupAddr[1] = f32;
                                    obj.setLinePhaseOff(f33);
                                }
                                break;
                            case 8:
                                content = editText8.getText().toString();
                                byte[] getBytes8 =HexStrUtil.hexStr2Bytes(content);
                                for(int i=1;i<getBytes8.length;i++){
                                    contentBytes[i] = getBytes8[i];
                                }
                                byte f1 = HexStrUtil.hexStr2Bytes(field1_param8_pw1.getText().toString())[0];
                                byte f2 = HexStrUtil.hexStr2Bytes(field2_param8_pw2.getText().toString())[0];
                                byte f3 = HexStrUtil.hexStr2Bytes(field3_param8_pw3.getText().toString())[0];
                                byte f4 = HexStrUtil.hexStr2Bytes(field4_param8_pw4.getText().toString())[0];
                                byte f5 = HexStrUtil.hexStr2Bytes(field5_param8_pw5.getText().toString())[0];
                                byte f6 = HexStrUtil.hexStr2Bytes(field6_param8_pw6.getText().toString())[0];
                                byte f7 = HexStrUtil.hexStr2Bytes(field7_param8_data1.getText().toString())[0];
                                byte f8 = HexStrUtil.hexStr2Bytes(field8_param8_data2.getText().toString())[0];
                                byte f9 = HexStrUtil.hexStr2Bytes(field9_param8_data3.getText().toString())[0];
                                byte f10 = HexStrUtil.hexStr2Bytes(field10_param8_data4.getText().toString())[0];
                                contentBytes[1] = f1;
                                contentBytes[2] = f2;
                                contentBytes[3] = f3;
                                contentBytes[4] = f4;
                                contentBytes[5] = f5;
                                contentBytes[6] = f6;
                                contentBytes[7] = f7;
                                contentBytes[8] = f8;
                                contentBytes[9] = f9;
                                contentBytes[10] = f10;
                                contentBytes[0] = 8;
                                break;
                            default:
                                content = editText1.getText().toString();
                                byte[] getBytes =HexStrUtil.hexStr2Bytes(content);
                                for(int i=1;i<getBytes.length;i++){
                                    contentBytes[i] = getBytes[i];
                                }
                                contentBytes[0] = (byte) (position+1);
                                break;
                        }
                        if(!content.contains("--")){
                            MinaClientThread.session.write(CMDUtil.setDeviceParam(HexStrUtil.hexStr2Bytes(addr), contentBytes));
                        }
                    }
                });

                AlertDialog ad = builder.create();
                ad.show();
                //下面两行代码加入后即可弹出输入法
                ad.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                ad.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            }
        });
    }

    private void initData() {
        RFDeviceInfo rf = MainCahce.deviceInfos.get(addr);
        terminalParams = new ArrayList<>();
        terminalParams.add(rf.getContent1() == null?"-- -- -- -- --":rf.getContent1());
        terminalParams.add(rf.getContent2() == null?"-- -- -- -- --":rf.getContent2());
        terminalParams.add(rf.getContent3() == null?"-- -- -- -- --":rf.getContent3());
        terminalParams.add(rf.getContent4() == null?"-- -- -- -- --":rf.getContent4());
        terminalParams.add(rf.getContent5() == null?"-- -- -- -- --":rf.getContent5());
        terminalParams.add(rf.getContent6() == null?"-- -- -- -- --":rf.getContent6());
        terminalParams.add(rf.getContent7() == null?"-- -- -- -- --":rf.getContent7());
        terminalParams.add(rf.getContent8() == null?"-- -- -- -- --":rf.getContent8());
    }
    static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            adapter.notifyDataSetChanged();
        }
    };
    public static void refreshDeviceList(String a){
        if(a.equals(addr)){
            RFDeviceInfo rf = MainCahce.deviceInfos.get(addr);
            terminalParams = new ArrayList<>();
            terminalParams.add(rf.getContent1() == null?"-- -- -- -- --":rf.getContent1());
            terminalParams.add(rf.getContent2() == null?"-- -- -- -- --":rf.getContent2());
            terminalParams.add(rf.getContent3() == null?"-- -- -- -- --":rf.getContent3());
            terminalParams.add(rf.getContent4() == null?"-- -- -- -- --":rf.getContent4());
            terminalParams.add(rf.getContent5() == null?"-- -- -- -- --":rf.getContent5());
            terminalParams.add(rf.getContent6() == null?"-- -- -- -- --":rf.getContent6());
            terminalParams.add(rf.getContent7() == null?"-- -- -- -- --":rf.getContent7());
            terminalParams.add(rf.getContent8() == null?"-- -- -- -- --":rf.getContent8());
            handler.sendMessage(new Message());
        }
    }
   static BaseAdapter adapter ;
    private void initView() {
        textView = (TextView) findViewById(R.id.text_view);
        listView = (ListView) findViewById(R.id.list_view);
        Intent intent = getIntent();
        String s = intent.getStringExtra("addr");
        textView.setText(textView.getText()+s);
        adapter = new TerminalParamAdapter();
        listView.setAdapter(adapter);
    }

    public static void actionStart(Context context, String addr) {
        Intent intent = new Intent(context, TerminalParam.class);
        intent.putExtra("addr", addr);
        context.startActivity(intent);
    }

    class TerminalParamAdapter extends BaseAdapter{

        public TerminalParamAdapter() {
            super();
        }

        @Override
        public int getCount() {
            return 8;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           String param =  terminalParams.get(position);
            View view;
            ViewHolder  viewHolder;
            if(convertView == null){
                view = LayoutInflater.from(MainActivity.context).inflate(R.layout.activity_terminal_param_item,null);
                viewHolder = new ViewHolder();
                viewHolder.param  = (TextView) view.findViewById(R.id.param);
                viewHolder.context  = (TextView) view.findViewById(R.id.context);
                view.setTag(viewHolder);
            }else{
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.param.setText("参数"+(position+1)+":");
            viewHolder.context.setText(param);
            return view;
        }
    }
     class ViewHolder{
        TextView param;
        TextView context;
    }
}
